// vue-cli
const webpack = require('webpack')
// nodejs提供的path库
const path = require('path')
/* 引入数据文件 */
const appData = require('./data.json')
const seller  = appData.seller
const goods   = appData.goods
const ratings = appData.ratings

function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    publicPath:  '/hungry/',
    productionSourceMap: false,
    css: {
        loaderOptions: {
            stylus: {
                'resolve url': true,
                'import': [
                    './src/theme'
                ]
            }
        }
    },
    pluginOptions: {
        'cube-ui': {
            postCompile: true,
            theme: true
        }
    },
    // 接口请求
    devServer: {
        before(app) {
            // 当浏览器请求/api/seller地址时
            app.get('/api/seller', function (req, res) {
                const id = req.query.id
                res.json({
                    errno: 0,
                    // 返回处理过的seller对象，并转化为json
                    data: Object.assign({}, seller, {id})
                })
            })
            app.get('/api/goods', function (req, res) {
                res.json({
                    errno: 0,
                    data: goods
                })
            })
            app.get('/api/ratings', function (req, res) {
                res.json({
                    errno: 0,
                    data: ratings
                })
            })
        }
    },
    // 配置文件别名
    chainWebpack(config) {
        config.resolve.alias
            .set('components', resolve('src/components'))
            .set('common', resolve('src/common'))
            .set('api', resolve('src/api'))
        // 去除moment的国际化，以减少打包体积
        config.plugin('context')
            .use(webpack.ContextReplacementPlugin,
                [/moment[/\\]locale$/, /zh-cn/])
    }
}
